package cn.itcast.bos.base.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.base.service.CourierService;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.Courier;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends  BaseAction<Courier>{
	@Autowired
	private CourierService courierService;
	private Integer[] paras;
	public void setParas(Integer[] paras) {
		this.paras = paras;
	}

	@Action(value="addCourier",results={@Result(name="success",location="/pages/base/courier.html",type="redirect")})
	public String addCourier(){
		courierService.addCourier(t);
		return SUCCESS;
	}
	
	@Action(value="deleteCourier")
	public String deleteCourier(){
		courierService.deleteCourier(paras);
		return NONE;
	}
	
	@Action(value="RestoreCourier")
	public String RestoreCourier(){
		courierService.RestoreCourier(paras);
		return NONE;
	}
	@Action(value="showCourier",results={@Result(name="success",type="json")})
	public String showCourier(){
		
		Specification specification=new Specification<Courier>() {
			@Override
			public Predicate toPredicate(Root<Courier> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list=new ArrayList<Predicate>();
//				courierNum:
				if(StringUtils.isNotBlank(t.getCourierNum())){
					Predicate p1 = cb.like(root.get("courierNum").as(String.class),"%"+t.getCourierNum()+"%");
					list.add(p1);
				}
//				company:
				if(StringUtils.isNotBlank(t.getCompany())){
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+t.getCompany()+"%");
					list.add(p2);
				}
//				type:
				if(StringUtils.isNotBlank(t.getType())){
					Predicate p3 = cb.like(root.get("type").as(String.class), "%"+t.getType()+"%");
					list.add(p3);
				}
//				standard.name:
				Join<Object, Object> join = root.join("standard",JoinType.INNER);
				if(t.getStandard()!=null&&StringUtils.isNotBlank(t.getStandard().getName())){
					Predicate p4 = cb.like(join.get("name").as(String.class),"%"+t.getStandard().getName()+"%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
			
		};

		Pageable pageable=new PageRequest(page-1, rows);
		Page<Courier> pagedata = courierService.showCourier(specification,pageable);
	    putIntoValueStack(pagedata);
		return SUCCESS;
	}
}
